package com.tanhua.manager.controller;

import cn.hutool.core.util.StrUtil;
import com.tanhua.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Struct;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping(value = "/manage/users", name = "用户列表")
    public ResponseEntity users(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize) {
        return userService.users(pageNum, pageSize);
    }

    @GetMapping(value = "/manage/users/{userId}", name = "用户详情")
    public ResponseEntity findByUserId(@PathVariable Long userId) {
        return userService.findByUserId(userId);
    }

    @GetMapping(value = "/manage/messages", name = "用户的动态列表")
    public ResponseEntity messages(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "uid", required = false) Long userId,
            String state) {
        Integer stateInt = null;
        if (StrUtil.isNotEmpty(state) &&  !state.equals("''")) {
            stateInt = Integer.parseInt(state);
        }
        return userService.messages(pageNum, pageSize, userId, stateInt);
    }

    @GetMapping(value = "/manage/messages/{publishId}", name = "动态详情")
    public ResponseEntity findByPublishId(@PathVariable String publishId) {
        return userService.findByPublishId(publishId);
    }

    @GetMapping(value = "/manage/messages/comments", name = "评论列表")
    public ResponseEntity comments(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "messageID") String publishId) {
        return userService.comments(pageNum, pageSize, publishId);
    }

    @GetMapping(value = "/manage/videos", name = "用户的小视频列表")
    public ResponseEntity videos(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "uid") Long userId) {
        return userService.videos(pageNum, pageSize, userId);
    }
}
